﻿using Sasa.QualityTools.CodeAnalysis.Common;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels.Events;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels
{
    public class ErrorReportViewModel : ViewModelBase
    {
        public event EventHandler<ShowWebPageRequiredEventArgs> ShowWebPageRequired;

        public event EventHandler CloseRequired;


        private FileVersionInfo visualStudioVersion;

        private Exception error;

        private string errorInfo;


        public FileVersionInfo VisualStudioVersion
        {
            get
            {
                return visualStudioVersion;
            }
            set
            {
                visualStudioVersion = value;
                OnPropertyChanged("VisualStudioVersion");
            }
        }

        public Exception Error
        {
            get
            {
                return error;
            }
            set
            {
                error = value;
                OnPropertyChanged("Error");
                ErrorInfo = Text.Format(value);
            }
        }

        public string ErrorInfo
        {
            get
            {
                return errorInfo;
            }
            set
            {
                errorInfo = value;
                OnPropertyChanged("ErrorInfo");
            }
        }

        public RelayCommand HelpCommand { get; private set; }

        public RelayCommand CopyErrorInfoCommand { get; private set; }

        public RelayCommand SendBugReportCommand { get; private set; }

        public RelayCommand CloseCommand { get; private set; }


        public ErrorReportViewModel()
        {
            HelpCommand = new RelayCommand(o => OnShowWebPageRequired("http://fxcopintegrator.codeplex.com/wikipage?title=Send%20Bug%20Report&referringTitle=Troubleshooting"));
            CopyErrorInfoCommand = new RelayCommand(o => CopyErrorInfo());
            SendBugReportCommand = new RelayCommand(o => OnShowWebPageRequired("http://fxcopintegrator.codeplex.com/WorkItem/Create"));
            CloseCommand = new RelayCommand(o => OnCloseRequired());
        }


        private void CopyErrorInfo()
        {
            var messages = new List<string>();
            messages.Add("**** System Information ****");
            messages.Add(Text.Format("{0} {1} ({2} core)", Environment.OSVersion.VersionString, (Environment.Is64BitOperatingSystem) ? "64bit" : "32bit", Environment.ProcessorCount));
            messages.Add(Text.Format("{0} {1} ({2}) {3}", VisualStudioVersion.ProductName, VisualStudioVersion.ProductVersion, VisualStudioVersion.FileVersion, VisualStudioVersion.Language));
            messages.Add(Text.Format("FxCop Integrator {0} ({1})", CommonConstants.ProductVersion, CommonConstants.FileVersion));
            messages.Add("");
            messages.Add("**** Error Information ****");
            messages.Add(ErrorInfo);
            messages.Add("");
            messages.Add("**** Trace Log ****");
            messages.Add(TraceLogger.Current.Logs);

            Clipboard.SetData(DataFormats.Text, String.Join(Environment.NewLine, messages));
        }

        protected virtual void OnShowWebPageRequired(string url)
        {
            if (ShowWebPageRequired != null)
            {
                ShowWebPageRequired(this, new ShowWebPageRequiredEventArgs(url));
            }
        }

        protected virtual void OnCloseRequired()
        {
            if (CloseRequired != null)
            {
                CloseRequired(this, EventArgs.Empty);
            }
        }
    }
}
